﻿
using Serilog;
using System.IO;
using System.Windows;

namespace EnvCheck;
using EnvCheck.Services;
using EnvCheck.View;
using System.Windows.Controls;

public partial class MainWindow : Window
{
    public void LoadFromSQLite(object sender, RoutedEventArgs e)
    {
        try
        {
            DatabaseService.ExecuteSqlScript("data.sql", false);
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Error executing SQL script: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        MessageBox.Show("更新完毕");
    }



    protected override void OnClosed(EventArgs e)
    {
        Log.Information("EnvCheck exiting.");
        Log.CloseAndFlush();
        base.OnClosed(e);
    }

    private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (e.Source is not TabControl tabControl)
        {
            Serilog.Log.Warning("TabControl_SelectionChanged called on non-TabControl instance.");
            return;
        }
        if (tabControl.SelectedItem is not BaseViewModel selectedTab)
        {
            Serilog.Log.Warning("TabControl_SelectionChanged called on TabControl without selected item.");
            Serilog.Log.Warning($"The class of the caller is: {tabControl.SelectedItem.GetType()}");
            return;
        }
        string header = selectedTab.VersionName;
        DatabaseService.ChangeSelectedVersion(header);

    }

}

